/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ixj_xmlprocess;
import ixj_entity.Customer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author LamHuynh
 */
public class CustomerXMLSearch {
    private String fileName;

    public CustomerXMLSearch(String fileName) {
        this.fileName = fileName;
    }
    public Boolean foundCustomer(String search) {
        if( search == null || search.isEmpty() )
            return false;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = factory.newDocumentBuilder();
            Document doc = parser.parse(new File(this.fileName));


            NodeList customers = doc.getElementsByTagName("customer");
            for (int i = 0; i < customers.getLength(); i++) {
                Node currentNode = customers.item(i);//Customer without (s)
                for (int j = 0; j < currentNode.getChildNodes().getLength(); j++) {
                    Node childNode = currentNode.getChildNodes().item(j);
                    if (childNode.getNodeName().equals("fname")) {
                        String fname = childNode.getTextContent();
                        if( fname.indexOf(search) != -1 )
                        {
                            return true;
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public ArrayList<Customer> getAllCustomerByName(String search) {
        ArrayList<Customer> results = new ArrayList<Customer>();
        if( search == null || search.isEmpty() )
            return results;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = factory.newDocumentBuilder();
            Document doc = parser.parse(new File(this.fileName));


            NodeList customers = doc.getElementsByTagName("customer");
            for (int i = 0; i < customers.getLength(); i++) {
                Node currentNode = customers.item(i);//Customer without (s)
                Customer c = new Customer();
                for (int j = 0; j < currentNode.getChildNodes().getLength(); j++) {//Di qua tung phan tu cua customer
                    Node childNode = currentNode.getChildNodes().item(j);
                    if (childNode.getNodeName().equals("fname")) {//Tung thuoc tinh
                        String fname = childNode.getTextContent();
                        c.setFname(fname);
                    }
                    if (childNode.getNodeName().equals("id")) {
                        String id = childNode.getTextContent();
                        c.setId(id);
                    }
                }
                if( c.getFname().indexOf(search) != -1 )
                {
                    results.add(c);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        return results;
    }
}
